package it.davial.mgwt.client.mvp;

import it.davial.mgwt.client.activities.about.AboutPlace;
import it.davial.mgwt.client.activities.menu.MenuPlace;
import it.davial.mgwt.client.activities.patient.PatientPlace;
import it.davial.mgwt.client.activities.picture.PicturePlace;
import it.davial.mgwt.client.activities.visit.VisitPlace;
import it.davial.mgwt.client.event.ActionEvent;
import it.davial.mgwt.client.event.ActionNames;
import it.davial.mgwt.client.event.MenuEntrySelectedEvent;
import it.davial.mgwt.client.event.MenuEntrySelectedEvent.MenuEntry;

import java.util.logging.Logger;

import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.History;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.HandlerRegistration;
import com.googlecode.mgwt.dom.client.event.mouse.HandlerRegistrationCollection;
import com.googlecode.mgwt.mvp.client.history.HistoryHandler;
import com.googlecode.mgwt.mvp.client.history.HistoryObserver;
import com.googlecode.mgwt.ui.client.MGWT;

public class AppHistoryObserver implements HistoryObserver {
	
	private static final Logger logger = Logger.getLogger("AppHistoryObserver");

	@Override
	public void onPlaceChange(Place place, HistoryHandler handler) {

	}

	@Override
	public void onHistoryChanged(Place place, HistoryHandler handler) {

	}

	@Override
	public void onAppStarted(Place place, HistoryHandler historyHandler) {
		if (MGWT.getOsDetection().isPhone()) {
			onPhoneNav(place, historyHandler);
		} else {
			// tablet
			onTabletNav(place, historyHandler);

		}

	}

	@Override
	public HandlerRegistration bind(EventBus eventBus, final HistoryHandler historyHandler) {

		HandlerRegistration register3 = MenuEntrySelectedEvent.register(eventBus, new MenuEntrySelectedEvent.Handler() {

			@Override
			public void onAnimationSelected(MenuEntrySelectedEvent event) {
				logger.info("onAnimationSelected");
				MenuEntry entry = event.getEntry();

				Place place = null;

				switch (entry) {
				case PATIENT:
					place = new PatientPlace();
					break;
				case VISIT:
					place = new VisitPlace();
					break;
				case MAIN:
					place = new PicturePlace();
					break;
		
				default:
					break;
				}

				if (MGWT.getOsDetection().isTablet()) {

					historyHandler.replaceCurrentPlace(place);
					historyHandler.goTo(place, true);
				} else {
					historyHandler.goTo(place);
				}

			}
		});

		HandlerRegistration register2 = ActionEvent.register(eventBus, ActionNames.BACK, new ActionEvent.Handler() {

			@Override
			public void onAction(ActionEvent event) {

				History.back();

			}
		});

		HandlerRegistration register = ActionEvent.register(eventBus, ActionNames.ANIMATION_END, new ActionEvent.Handler() {

			@Override
			public void onAction(ActionEvent event) {
				if (MGWT.getOsDetection().isPhone()) {
					History.back();
				} else {
					historyHandler.goTo(new MenuPlace(), true);
				}

			}
		});

		HandlerRegistrationCollection col = new HandlerRegistrationCollection();
		col.addHandlerRegistration(register);
		col.addHandlerRegistration(register2);
		col.addHandlerRegistration(register3);
		return col;
	}

	private void onPhoneNav(Place place, HistoryHandler historyHandler) {
		if (place instanceof VisitPlace || place instanceof PatientPlace || place instanceof PicturePlace || place instanceof AboutPlace){

			historyHandler.replaceCurrentPlace(new AboutPlace());

			historyHandler.pushPlace(new MenuPlace());

		} else {
			if (place instanceof VisitPlace) {
				historyHandler.replaceCurrentPlace(new AboutPlace());

			} else {
				if (place instanceof PatientPlace) {
					historyHandler.replaceCurrentPlace(new AboutPlace());
				
				}
			}
		}
		
	}

	private void onTabletNav(Place place, HistoryHandler historyHandler) {
		if (place instanceof VisitPlace || place instanceof PatientPlace || place instanceof PicturePlace ) {

			historyHandler.replaceCurrentPlace(new AboutPlace());

		} else {
			if (place instanceof MenuPlace) {
				historyHandler.replaceCurrentPlace(new MenuPlace());
			
				}
			}
		
	}

}
